<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>提取分组</title>
</head>
<body>
  
  <script type="text/javascript">
    var str = '<div>我是{{name}}今年{{ age }}</div>',
        pattern = /{{\s*([a-zA-Z]+)\s*}}/;
    var replace = pattern.exec(str);

    var str2 = str.replace(replace[0], replace[1]),
        patt2 = pattern.exec(str2);

    // console.log(str2.replace(patt2[0], patt2[1]))
   
    /**
     * fval: Regex
     * sval: String
    */
    function callback(fval, sval){
      var pattern = null;
      while(pattern = fval.exec(sval)){
        sval = sval.replace(pattern[0], pattern[1]);
      }
      return sval;
    }

    function callbackData(fval, str, data){
      var patt = null;
      while(patt = fval.exec(str)){
        str = str.replace(patt[0], data[patt[1]]);
      }
      return str;
    }

    console.log(callback(pattern, str));
    console.log(callbackData(pattern, str, {name: "张三", age: "18"}));

  </script>
</body>
</html>